package com.yanqu.road.server.cmd.house;

import com.yanqu.road.entity.config.player.HouseCreditInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.house.HouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.HouseModule;
import com.yanqu.road.server.manger.config.HouseMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_HOUSE_CREDIT_UPGRADE, desc = "钱庄荣誉升级")
public class HouseCreditUpgradeCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HouseProto.HouseCreditUpgradeReqMsg reqMsg = HouseProto.HouseCreditUpgradeReqMsg.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		HouseCreditInfo houseCreditInfo = HouseMgr.getHouseCreditInfo(player.getUserInfo().getHouseCreditLv());
		HouseCreditInfo nextHouseCreditInfo = HouseMgr.getHouseCreditInfo(player.getUserInfo().getHouseCreditLv() + 1);
		if (nextHouseCreditInfo == null) {
			ret = GameErrorCode.E_HOUSE_CREDIT_NO_EXIT;
			return;
		}

		if(!player.getModule(HouseModule.class).isEnoughHouseCredit(houseCreditInfo.getCondition(),eLogMoneyType.HouseBank,eLogMoneyType.HouseBankCreditUpgrade)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}


		player.getModule(HouseModule.class).houseCreditUpgrade();
		player.getModule(HouseModule.class).syncUserHouseBank();

	}

	@Override
	public void sendData(GamePlayer player) {
		HouseProto.HouseCreditUpgradeRespMsg.Builder respMsg = HouseProto.HouseCreditUpgradeRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_HOUSE_CREDIT_UPGRADE, respMsg);
	}
}
